<?php
// +----------------------------------------------------------------------
// | Created by Sublime: [ Conqweal ]
// +----------------------------------------------------------------------
// | Copyright (c) 2021~20223[chenjia] All rights reserved.
// +----------------------------------------------------------------------
// | SiteUrl: http://www.99dn.cn
// +----------------------------------------------------------------------
// | Author: Conqweal <150672834@qq.com>
// +----------------------------------------------------------------------
// | Date: 2021/12/22 0026
// +----------------------------------------------------------------------
// | Description:信息分类
// +----------------------------------------------------------------------

namespace app\admin\controller;
use app\BackendController;
use app\admin\model\Seo as cmodel;
use think\facade\Cache;
use support\Request;
use think\facade\Db;


class SeoController extends BackendController
{

 
     
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
        $pid = $request->get('pid/d', 0);
        return view('seo/index', compact("pid"));
    }

    public function listData(Request $request)
    {
       
        
        
        if ($request->get('title')) {
            $w[] = ['a.title', 'like', '%' . $request->get('title') . '%'];

        }   
        if ($request->get('url')) {
            $w[] = ['a.url', 'like', '%' . $request->get('url') . '%'];

        }
         
        $q = cmodel::alias('a')->join("seo_description b", "a.id=b.id and b.language_id=" . $this->lang_id, 'left')->where($w);
        $ls = $q->field("a.id,a.title,a.title as name,a.url")->select()->toArray();  
       
        
        return json(['code' => count($ls) > 0 ? 0 : 201, 'msg' => '无数据', 'count' => count($ls), 'data' => $ls]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {

        $menus = cmodel::with("description")->select();
        $pid  =request()->get("pid");
        
        if (!$menus->isEmpty()) {
            $menus_new = [];
            foreach ($menus as $k => $v) {

                $menus_new[$v['id']] = [
                    'id'       => $v['id'],
                    'parentid' => $v['pid'],                   
                    'order'    => 0,
                    'name'     => $v['title'],
                ];
            }

            

        }

        $langs = getLangs();

        return view('seo/create', compact('langs'));
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $post  = $request->post();
        
        $valid = new \app\admin\validate\Seo;
        $r     = $valid->check($post);
        if (!$r) {
            $msg = ['status' => -1, 'msg' => $valid->getError()];
        } else {
            $cmodel = new cmodel;
           
            $cmodel->save($post);
            $id = $cmodel->id;
            //多语言处理

            array_walk($post['description'], function (&$v, $k) use ($id) {
                $v['id']      = $id;
                $v['language_id'] = $k;
            });

            $cmodel->description()->saveAll($post['description']);
            $cmodel->save($post);

            $msg = ['status' => 1, 'msg' => '保存成功'];
        }
        return json($msg);
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit(Request $request)
    {   
        $id  = $request->get("id");
        
        $data        = cmodel::with("description")->find($id);       
        $description = array_column($data->description->toArray(), null, 'language_id');
        $langs = getLangs(); 
        return view('seo/edit', compact('data', 'langs', 'description'));
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request)
    {
        $id  = $request->get("id");
        $post   = $request->post();
        $valid  = new \app\admin\validate\Seo;
        $r      = $valid->check($post);
        $cmodel = new cmodel;
        $main   = $cmodel->where('id', $id)->find();
        if (!$r) {
            $msg = ['status' => -1, 'msg' => $valid->getError()];
        } else {

            //多语言处理
            $main->description()->where("id", $id)->delete();
            array_walk($post['description'], function (&$v, $k) use ($id) {
                $v['id']      = $id;
                $v['language_id'] = $k;
            });

            $main->description()->saveAll($post['description']);
         
            $main->save($post);

            $msg = ['status' => 1, 'msg' => '保存成功'];
        }
        return json($msg);
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete(Request $request)
    {
        $id = $request->get("id");

        $ids = is_string($id) ? explode(",", $id) : $id;

        foreach ((array) $ids as $id) {

            $info = cmodel::with(['description'])->find($id);
            $info->together(['description'])->delete();
            unset($info);
        }

        return json(['msg' => '删除成功', 'status' => 1]);
    }
}
